package com.health.platform.provider.symptommonitor.service;

import com.health.platform.common.core.entity.CommonQueryEntity;
import com.health.platform.common.restful.AppResponse;
import com.health.platform.common.core.entity.EchartsEntity;
import com.health.platform.provider.symptommonitor.entity.EchartsMoreEntity;
import com.health.platform.common.utils.MyUtils;
import com.health.platform.provider.symptommonitor.dao.SymptomMonitorDao;
import com.health.platform.provider.symptommonitor.entity.SummaryEntity;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 病症监控-症候群监控分析（地方）service
 *
 * @author shi_xy_sr@neusiri.com
 * @date 2021/3/6 13:42
 */
@Service
public class SymptomMonitorService {
    @Resource
    private SymptomMonitorDao symptomMonitorDao;
    /**
     * 查询人群类别码表
     *
     * @param
     * @return 结果
     * @author shi_xy_sr@neusiri.com
     * @date 2021/3/6 14:17
     */
    public AppResponse getDict() {
        return AppResponse.success("查询成功", symptomMonitorDao.getDict());
    }
    /**
     * 获取中间部分汇总信息 (就诊人次与同比、发热人次与同比、呼吸系统疾病就诊人次与同比、呼吸道症状就诊人次与同比)
     *
     * @param
     * @return 结果
     * @author shi_xy_sr@neusiri.com
     * @date 2021/3/6 14:43
     */
    public AppResponse getSummaryInfo(CommonQueryEntity symptomMonitorEntity) {
        Map<String, Object> result = new HashMap<>();
        //——————————————————————就诊人次、发热人次、呼吸系统疾病就诊人次、呼吸道症状就诊人次——————————————————————
        symptomMonitorEntity.setCompare(false);
        SummaryEntity thisYearSummary = symptomMonitorDao.getSummaryInfo(symptomMonitorEntity);
        //——————————————————————就诊人次同比、发热人次同比、呼吸系统疾病就诊人次同比、呼吸道症状就诊人次同比——————————————————————
        //查询往年同期
        symptomMonitorEntity.setCompare(true);
        SummaryEntity lastYearSummary = symptomMonitorDao.getSummaryInfo(symptomMonitorEntity);
        //求比例
        if(thisYearSummary != null && lastYearSummary !=null){
            String jzCompare = MyUtils.getProportion(thisYearSummary.getJzNum(), lastYearSummary.getJzNum());//就诊人次同比
            String frCompare = MyUtils.getProportion(thisYearSummary.getFrNum(), lastYearSummary.getFrNum());//发热人次同比
            String hxxtjbjzCompare = MyUtils.getProportion(thisYearSummary.getHxxtjbjzNum(), lastYearSummary.getHxxtjbjzNum());//呼吸系统疾病就诊人次同比
            String hxdzzjzCompare = MyUtils.getProportion(thisYearSummary.getHxdzzjzNum(), lastYearSummary.getHxdzzjzNum());//呼吸道症状就诊人次同比
            thisYearSummary.setJzCompare(jzCompare);
            thisYearSummary.setFrCompare(frCompare);
            thisYearSummary.setHxxtjbjzCompare(hxxtjbjzCompare);
            thisYearSummary.setHxdzzjzCompare(hxdzzjzCompare);
        }
        result.put("summaryInfo",thisYearSummary);
        return AppResponse.success("查询成功", result);
    }
    /**
     * 获取疾病top(呼吸道疾病top10与上升top10、呼吸道症状top10)
     *
     * @param
     * @return 结果
     * @author shi_xy_sr@neusiri.com
     * @date 2021/3/8 9:12
     */
    public AppResponse getDiseaseTopten(CommonQueryEntity symptomMonitorEntity) {
        Map<String, Object> result = new HashMap<>();
        //——————————————————————呼吸道疾病top10与上升top10——————————————————————
        //呼吸道疾病
        symptomMonitorEntity.setTargetType("YL_MZ_TYPE26");
        List<EchartsMoreEntity> hxdjb = symptomMonitorDao.getEchartsMZByDisease(symptomMonitorEntity);
        result.put("hxdjb",hxdjb);
        //上升top10
        List<EchartsMoreEntity> hxdjbcompare = symptomMonitorDao.getEchartsMZByDiseaseUp(symptomMonitorEntity);
        result.put("hxdjbcompare",hxdjbcompare);
        //——————————————————————呼吸道症状top10——————————————————————
        symptomMonitorEntity.setTargetType("YL_MZ_TYPE27");
        List<EchartsMoreEntity> hxdzz = symptomMonitorDao.getEchartsMZByDisease(symptomMonitorEntity);
        result.put("hxdzz",hxdzz);
        return AppResponse.success("查询成功", result);
    }

    /**
     * 根据地点分组获得统计数据（全域呼吸道疾病就诊人次占比对比、全域症候群就诊人次占比对比）
     *
     * @param
     * @return 结果
     * @author shi_xy_sr@neusiri.com
     * @date 2021/3/9 8:51
     */
    public AppResponse getDiseaseByArea(CommonQueryEntity symptomMonitorEntity) {
        Map<String, Object> result = new HashMap<>();
        if(symptomMonitorEntity.getSgbCode().length() == 2){//省
            symptomMonitorEntity.setSubstrIndex(4);
        }else if(symptomMonitorEntity.getSgbCode().length() == 4){//市
            symptomMonitorEntity.setSubstrIndex(6);
        }else if(symptomMonitorEntity.getSgbCode().length() == 6){//区
            symptomMonitorEntity.setSubstrIndex(8);
        }
        List<EchartsEntity> diseaseArea = symptomMonitorDao.getDiseaseFlowByArea(symptomMonitorEntity);
        result.put("diseaseArea",diseaseArea);
        return AppResponse.success("查询成功", result);
    }
    /**
     * 获得七日统计数据（呼吸道疾病近七日就诊趋势、近七日发热人次趋势）
     *
     * @param
     * @return 结果
     * @author shi_xy_sr@neusiri.com
     * @date 2021/3/10 14:35
     */
    public AppResponse getDiseaseByDay(CommonQueryEntity symptomMonitorEntity) {
        Map<String, Object> result = new HashMap<>();
        symptomMonitorEntity.setTablename("TARGET_MZ_TABLE");
        symptomMonitorEntity.setDateFormat("yyyy-MM-dd");
        symptomMonitorEntity.setConditionRecentDateStr(symptomMonitorEntity.getConditionRecentDayDateStr());
        symptomMonitorEntity.setConditionRecentCompareDateStr(symptomMonitorEntity.getConditionRecentDayCompareDateStr());
        //——————————————————————近七日发热人次趋势——————————————————————
        symptomMonitorEntity.setTargetType("YL_MZ_TYPE25");
        List<EchartsEntity> frqs = symptomMonitorDao.getEchartsMZByTime(symptomMonitorEntity);
        frqs = MyUtils.getEchartsDataByYear(frqs,symptomMonitorEntity.getRecentDayArr());
        result.put("frqs",frqs);
        //——————————————————————呼吸道疾病近七日就诊趋势——————————————————————
        //呼吸道疾病
        symptomMonitorEntity.setTargetType("YL_MZ_TYPE26");
        List<EchartsEntity> hxdjb = symptomMonitorDao.getEchartsMZByTime(symptomMonitorEntity);
        hxdjb = MyUtils.getEchartsDataByYear(hxdjb,symptomMonitorEntity.getRecentDayArr());
        result.put("hxdjb",hxdjb);
        //呼吸道疾病平均数
        String hxdjbAvg = MyUtils.getAverage(hxdjb);
        result.put("hxdjbAvg",hxdjbAvg);
        //呼吸道疾病同比
        symptomMonitorEntity.setCompare(true);
        List<EchartsEntity> hxdjbtb = symptomMonitorDao.getEchartsMZByTime(symptomMonitorEntity);
        hxdjbtb = MyUtils.getEchartsDataByYear(hxdjbtb,symptomMonitorEntity.getRecentDayCompareArr());
        result.put("hxdjbtb",hxdjbtb);
        return AppResponse.success("查询成功", result);
    }
    /**
     * 症候群就诊近7日趋势
     *
     * @param
     * @return 结果
     * @author shi_xy_sr@neusiri.com
     * @date 2021/3/15 15:14
     */
    public AppResponse getDiseaseByDayByDisease(CommonQueryEntity symptomMonitorEntity) {
        Map<String, Object> result = new HashMap<>();
        symptomMonitorEntity.setTablename("TARGET_MZ_TABLE");
        symptomMonitorEntity.setDateFormat("yyyy-MM-dd");
        symptomMonitorEntity.setConditionRecentDateStr(symptomMonitorEntity.getConditionRecentDayDateStr());
        symptomMonitorEntity.setConditionRecentCompareDateStr(symptomMonitorEntity.getConditionRecentDayCompareDateStr());
        //今年
        List<EchartsEntity> resultLine = symptomMonitorDao.getEchartsMZByTime(symptomMonitorEntity);
        resultLine = MyUtils.getEchartsDataByYear(resultLine,symptomMonitorEntity.getRecentDayArr());
        //平均数
        String resultLineAvg = MyUtils.getAverage(resultLine);
        result.put("resultLineAvg",resultLineAvg);
        //同比
        symptomMonitorEntity.setCompare(true);
        List<EchartsEntity> resultLinetb = symptomMonitorDao.getEchartsMZByTime(symptomMonitorEntity);
        resultLinetb = MyUtils.getEchartsDataByYear(resultLinetb,symptomMonitorEntity.getRecentDayCompareArr());
        result.put("resultLine",resultLine);
        result.put("resultLinetb",resultLinetb);
        return AppResponse.success("查询成功", result);
    }
    /**
     * 哨点数据监测
     * 
     * @param 
     * @return 结果
     * @author shi_xy_sr@neusiri.com
     * @date 2021/3/15 15:57
     */
    public AppResponse getDiseaseByCycle(CommonQueryEntity symptomMonitorEntity) {
        Map<String, Object> result = new HashMap<>();
        String[] getRecentArr = null;
        String[] getRecentCompareArr = null;
        if(symptomMonitorEntity.getLineType() == 1){//按日<近7日>
            symptomMonitorEntity.setConditionRecentDateStr(symptomMonitorEntity.getConditionRecentDayDateStr());
            symptomMonitorEntity.setConditionRecentCompareDateStr(symptomMonitorEntity.getConditionRecentDayCompareDateStr());
            getRecentArr = symptomMonitorEntity.getRecentDayArr();
            getRecentCompareArr = symptomMonitorEntity.getRecentDayCompareArr();
        }else if(symptomMonitorEntity.getLineType() == 2){//周<近12周>
            symptomMonitorEntity.setConditionRecentDateStr(symptomMonitorEntity.getConditionRecentWeekDateStr());
            symptomMonitorEntity.setConditionRecentCompareDateStr(symptomMonitorEntity.getConditionRecentWeekCompareDateStr());
            getRecentArr = symptomMonitorEntity.getRecentWeekArr();
            getRecentCompareArr = symptomMonitorEntity.getRecentWeekCompareArr();
        }else if(symptomMonitorEntity.getLineType() == 3){//月<近12个月>
            symptomMonitorEntity.setConditionRecentDateStr(symptomMonitorEntity.getConditionRecentMonthDateStr());
            symptomMonitorEntity.setConditionRecentCompareDateStr(symptomMonitorEntity.getConditionRecentMonthCompareDateStr());
            getRecentArr = symptomMonitorEntity.getRecentMonthArr();
            getRecentCompareArr = symptomMonitorEntity.getRecentMonthCompareArr();
        }else if(symptomMonitorEntity.getLineType() == 4){//年<近5年
            symptomMonitorEntity.setConditionRecentDateStr(symptomMonitorEntity.getConditionRecentYearDateStr());
            symptomMonitorEntity.setConditionRecentCompareDateStr(symptomMonitorEntity.getConditionRecentYearCompareDateStr());
            getRecentArr = symptomMonitorEntity.getRecentYearArr();
            getRecentCompareArr = symptomMonitorEntity.getRecentYearCompareArr();
        }
        if(symptomMonitorEntity.getTargetType().equals("YL_ZY_TYPE21")){//院内感染
            symptomMonitorEntity.setTablename("TARGET_ZY_TABLE");
        }else if(symptomMonitorEntity.getTargetType().equals("GW_TYPE28")){//因病缺课
            symptomMonitorEntity.setTablename("TARGET_GW_TABLE");
        }else{
            symptomMonitorEntity.setTablename("TARGET_MZ_TABLE");
        }
        //今年
        List<EchartsEntity> resultLine = symptomMonitorDao.getEchartsMZByTime(symptomMonitorEntity);
        resultLine = MyUtils.getEchartsDataByYear(resultLine,getRecentArr);
        //平均数
        String resultLineAvg = MyUtils.getAverage(resultLine);
        result.put("resultLineAvg",resultLineAvg);
        //同比
        symptomMonitorEntity.setCompare(true);
        List<EchartsEntity> resultLinetb = symptomMonitorDao.getEchartsMZByTime(symptomMonitorEntity);
        resultLinetb = MyUtils.getEchartsDataByYear(resultLinetb,getRecentCompareArr);
        result.put("resultLine",resultLine);
        result.put("resultLinetb",resultLinetb);
        return AppResponse.success("查询成功", result);
    }
}
